package com.sita.tingterest.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.tingterest.pojo.Music;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

/**
 * Created by nilesh on 11/7/2015.
 */
public class GridViewAdapter extends BaseAdapter {


    private Activity activity;
    private ArrayList<Music> MusicList = null;

    public GridViewAdapter(FragmentActivity activity, ArrayList<Music> musicList) {
        this.activity = activity;
        this.MusicList = musicList;

    }

    @Override
    public int getCount() {

        Log.i("MY TAG", "MusicList.size();" + MusicList.size());

        return MusicList.size();
    }

    @Override
    public Object getItem(int position) {
        return MusicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public static class ViewHolder {
        public ImageView img;
        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view = null;
        LayoutInflater inflator = activity.getLayoutInflater();
        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.ting_grid_row, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.title_song);
            view.img = (ImageView) convertView.findViewById(R.id.pic);

            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();

        }
        // Log.i("MY TAG","MusicList.get(position).getAuthorId()"+MusicList.get(position).getAuthorId());
        view.txtViewTitle.setText(MusicList.get(position).getTitle());
        //Log.i("MY TAG", "MusicList.get(position). getImage()" + MusicList.get(position).getImage());

        new GetImage(view, convertView).execute(MusicList.get(position).getPic().getUrl());


        return convertView;
    }

    private class GetImage extends AsyncTask<String, String, Bitmap> {
        private byte[] iconData;
        private ViewHolder view;
        private View converView;
        private Bitmap bmp;


        public GetImage(ViewHolder view, View convertView)

        {
            Log.i("MY TAG", "GetImage in AsynTask");
            this.view = view;
            this.converView = convertView;
        }

        @Override
        protected Bitmap doInBackground(String... params) {


            try {
                InputStream in = new URL(params[0]).openStream();
                Log.i("MY TAG", "params[0]" + params[0]);
                bmp = BitmapFactory.decodeStream(in);
            } catch (Exception e) {

            }
            return bmp;

        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {

            view.img.setImageBitmap(bitmap);

            super.onPostExecute(bitmap);
        }
    }
}
